﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DunSilverlight.Web.Comm;
using DunLibrary.User;
using DunSilverlight.Web.Serializable;
using NHibernate;
using System.ServiceModel.Activation;

namespace DunSilverlight.Web.UserWcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“OperationLogService”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class OperationLogService :BaseService<OperationLog>, IOperationLogService
    {
        /// <summary>
        /// 新增一个日志
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public override string SaveOrUpdate(string json)
        {
            OperationLog oLog = JsonSerializerHelper.JsonToEntity<OperationLog>(json);
            oLog.OpTime = DateTime.Now;//写入服务器端时间

            using (ISession sess = HbmSessionFactory.OpenSession())
            {
                ITransaction tx = null;
                try
                {
                    tx = sess.BeginTransaction();
                    sess.SaveOrUpdate(oLog);
                    tx.Commit();
                    return PackJsonResult("true", string.Empty, string.Empty);
                }
                catch (Exception ex)
                {
                    if(tx.IsActive)
                        tx.Rollback();
                    return PackJsonResult("false", string.Empty, ex.Message); 
                }
            }

            
        }
    }
}
